/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.city;

import cz.muni.fi.diplomka.servicelayer.dto.BuildingInCity;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityBuildingManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.PlayerManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityBuildingManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.PlayerManagerImpl;
import cz.muni.fi.diplomka.webapp.building.BuildingPage;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import cz.muni.fi.diplomka.webapp.resource.ResourcesPanel;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.resource.ContextRelativeResource;

/**
 *
 * @author Jakub Kutil
 */
public class CityPage extends WebPage {

    private CityBuildingManager cityBuildingManager = new CityBuildingManagerImpl();
    private CityManager cityManager = new CityManagerImpl();
    private PlayerManager playerManager = new PlayerManagerImpl();
    private String selectedCity = "";
    private List<String> cities;

    public CityPage(final PageParameters parameters) {
        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));


            PageParameters p = new PageParameters();
            p.add("id", id.toString());

            add(new LinkPanel("linkPanel", p));


            cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }

            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            p.add("cityName", selectedCity);

            add(new ResourcesPanel("resourcesPanel", p));

            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            City c = cityManager.getCityByName(selectedCity);
            add(new Label("cityInfo", "City " + c.getName() + " at [" + c.getCoordinates().getX_coordinate() + ","
                    + c.getCoordinates().getY_coordinate() + "] owned by " + playerManager.findPlayerById(id).getTitle().toString() + " " + playerManager.findPlayerById(id).getNickName()));
           
            final TextField<String> cityName = new TextField<>("cityName", Model.of(cityManager.getCityByName(selectedCity).getName()));
                       
            Form<?> form = new Form<Void>("form") {
                @Override
                protected void onSubmit() {
                    if (cityManager.getCityByName(cityName.getValue()).getPlayerId() != null) {
                        error("City name already taken");
                        return;
                    }
                    City city = cityManager.getCityByName(selectedCity);
                    city.setName(cityName.getValue());

                    cityManager.editCity(city);
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("cityName", city.getName());
                    setResponsePage(CityPage.class, pageParameters);
                }
            };

            Form<?> selectCity = new Form<Void>("selectCity") {
                @Override
                protected void onSubmit() {
                    selectedCity = cities.get(Integer.parseInt(city.getValue()));
                    PageParameters pageParameters = new PageParameters();
                    pageParameters.add("id", id.toString());
                    pageParameters.add("cityName", selectedCity);
                    setResponsePage(CityPage.class, pageParameters);
                }
            };

            add(selectCity);
            selectCity.add(city);

            add(form);
            form.add(cityName);

            for (BuildingInCity b : cityBuildingManager.getAllBuildingsInCity(cityManager.getCityByName(selectedCity).getId())) {
                PageParameters pageParameters = new PageParameters();
                BuildingInCity b1 = new BuildingInCity();

                b1.setCoordinates(b.getCoordinates());
                b1.setName(b.getName());
                b1.setCityId(b.getCityId());
                b1.setLevel(b.getLevel());

                pageParameters.add("id", id.toString());
                pageParameters.add("cityId", b1.getCityId().toString());
                pageParameters.add("x", String.valueOf(b1.getCoordinates().getX_coordinate()));
                pageParameters.add("y", String.valueOf(b1.getCoordinates().getY_coordinate()));

                BookmarkablePageLink<Void> link = new BookmarkablePageLink<>("x" + b1.getCoordinates().getX_coordinate() + "y" + b1.getCoordinates().getY_coordinate(), BuildingPage.class, pageParameters);

                link.add(new Image("id", new ContextRelativeResource("/images/building/" + b1.getName() + ".png")));

                add(link);
            }
        }
    }
}
